package com.survey.service.impl;

import com.github.pagehelper.PageHelper;
import com.survey.dao.mapper.QuestionMapper;
import com.survey.pojo.data.QuestionDO;
import com.survey.service.QuestionService;
import com.survey.pojo.vo.QuestionVO;
import com.survey.utils.PageBean;
import com.survey.utils.Result;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/** 
 * <br/>
 * Created by jj on 2018/08/04
 */
@Service("questionService")
public class QuestionServiceImpl implements QuestionService {

	private final static Logger LOG = LoggerFactory.getLogger(QuestionServiceImpl.class);

	@Resource
	private QuestionMapper questionMapper;

	@Override
	public Result addQuestion(QuestionVO questionVO) throws Exception {
		Integer row = questionMapper.addQuestion(questionVO);
		if(row>0){
			return new Result(Result.CODE_SUCCESS);
		}
		return new Result(Result.CODE_FAILURE);
	}

	@Override
	public Result updateQuestion(QuestionVO questionVO) throws Exception {
		return null;
	}
	
	@Override
	public QuestionDO findDetailQuestion(QuestionVO questionVO) throws Exception{
	   return questionMapper.findDetailQuestion(questionVO);
	}

	@Override
	public Result listQuestion(QuestionVO questionVO) throws Exception{
		return null;
	}
	
	@Override
	public Result listQuestionPage(QuestionVO questionVO) throws Exception{
		return null;
	}
	
	@Override
	public Result countQuestion(QuestionVO questionVO) throws Exception{
		return null;
	}
	
	@Override
	public Result deleteQuestion(QuestionVO questionVO) throws Exception{
		Integer row = questionMapper.deleteQuestion(questionVO);
		if(row>0){
			return new Result(Result.CODE_SUCCESS);
		}
		return new Result(Result.CODE_FAILURE);
	}

	@Override
	public PageBean<QuestionVO> listQuestionBySurveyId(QuestionVO questionVO) throws InvocationTargetException, IllegalAccessException {
		Integer count = questionMapper.countQuestion(questionVO);
		PageHelper.startPage(questionVO.getPageCode(),questionVO.getPageSize());
		List<QuestionDO> questionDOS = questionMapper.listQuestionBySurveyId(questionVO);
		ArrayList<QuestionVO> list=new ArrayList<>();
		for (QuestionDO questionDO : questionDOS) {
			QuestionVO vo=new QuestionVO();
			BeanUtils.copyProperties(vo,questionDO);
			list.add(vo);
		}
		PageBean<QuestionVO> pageBean=new PageBean<>(
				list,questionVO.getPageSize(),questionVO.getPageCode(),count,questionVO.getSize()
		);
		return pageBean;
	}

}